import cv2
import numpy as np
cv2.__version__
'4.0.1'
# Declare a variable with the path to your image
img = cv2.imread('annotations.jpg')
# Load the image with openCV
# What is the image type? -> type
type(img)
numpy.ndarray
# Print the value of the pixels of the image
img
array([[[ 50, 56, 55],
[ 52, 58, 57],
[ 54, 60, 59],
...,
[ 35, 95, 137],
[ 27, 54, 91],
[ 56, 68, 62]],
[[ 51, 57, 56],
[ 52, 58, 57],
[ 54, 60, 59],
...,
[ 28, 80, 120],
[ 32, 53, 85],
[ 55, 63, 56]],
[[ 51, 57, 56],
[ 52, 58, 57],
[ 54, 60, 59],
...,
[ 14, 56, 91],
[ 44, 57, 83],
[ 56, 57, 47]],
...,
[[177, 166, 158],
[182, 171, 163],
[183, 172, 164],
...,
[ 61, 63, 74],
[ 40, 42, 53],
[ 48, 50, 60]],
[[179, 168, 160],
[181, 170, 162],
[178, 167, 159],
...,
[ 61, 62, 76],
[ 37, 39, 50],
[ 49, 51, 61]],
[[181, 171, 161],
[178, 168, 158],
[172, 162, 152],
...,
[ 60, 61, 75],
[ 30, 32, 43],
[ 42, 44, 55]]], dtype=uint8)
# Lets now see the image! dont forget to call destroyAllWindows!
cv2.imshow('My Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
-1
# Import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
# Choose a pyplot figsize to make images fit nicely into the notebook
plt.figure( figsize = (20,15))
# Now display the image with plt
plt.imshow(img)
<matplotlib.image.AxesImage at 0x16f2ba7a8b0>
# Does your image look ok? are the colors the right ones?, if not, lets fix it!
# Remember that openCV will load images using BGR rather than RGB
# Change the image to RGB and diplay it again with plt
rgb_img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure( figsize = (20,15))
plt.imshow(rgb_img)
<matplotlib.image.AxesImage at 0x16f2c2ee970>
# Check the image shape
img.shape
(576, 768, 3)
# Load the the image in grayscale, what are the 2 ways you can do that?
grayscale_img = cv2.imread('annotations.jpg', cv2.IMREAD_GRAYSCALE)
# Now load an image in color and then transform it to grayscale
grayscale_img = cv2.imread('annotations.jpg', cv2.IMREAD_GRAYSCALE)
# Check the grayscale shape
grayscale_img
array([[ 55, 57, 59, ..., 101, 62, 65],
[ 56, 57, 59, ..., 86, 60, 60],
[ 56, 57, 59, ..., 62, 63, 54],
...,
[165, 170, 171, ..., 66, 45, 53],
[167, 169, 166, ..., 66, 42, 54],
[169, 166, 160, ..., 65, 35, 47]], dtype=uint8)
# Display the grayscale image with matplotlib, how does it look?
plt.figure( figsize = (20,15))
plt.imshow(grayscale_img)
<matplotlib.image.AxesImage at 0x16f2c3a5bb0>
# Display the grayscale image with matplotlib, make sure to include the colormap so it really is grayscale
plt.figure( figsize = (20,15))
plt.imshow(grayscale_img, cmap='gray')
<matplotlib.image.AxesImage at 0x16f2c40bd60>
# Lets now crop a Region of Interest of an image, load and image and crop different parts of it
cropped_img = rgb_img[0:250, 0:600]
# Save to disk the cropped areas
cv2.imwrite('sky_crop.jpg',cropped_img )
True
# Load an image and then make a copy of it
loaded_img = cv2.imread('sky_crop.jpg')
plt.figure( figsize = (20,15))
plt.imshow(loaded_img)
<matplotlib.image.AxesImage at 0x16f2d487fa0>
# Draw green rectangle on top of a region of interest in your image
green =(0,255,0)
# Make a copy of your image and then write some text on top of the image
clone = rgb_img.copy()
clone
array([[[ 55, 56, 50],
[ 57, 58, 52],
[ 59, 60, 54],
...,
[137, 95, 35],
[ 91, 54, 27],
[ 62, 68, 56]],
[[ 56, 57, 51],
[ 57, 58, 52],
[ 59, 60, 54],
...,
[120, 80, 28],
[ 85, 53, 32],
[ 56, 63, 55]],
[[ 56, 57, 51],
[ 57, 58, 52],
[ 59, 60, 54],
...,
[ 91, 56, 14],
[ 83, 57, 44],
[ 47, 57, 56]],
...,
[[158, 166, 177],
[163, 171, 182],
[164, 172, 183],
...,
[ 74, 63, 61],
[ 53, 42, 40],
[ 60, 50, 48]],
[[160, 168, 179],
[162, 170, 181],
[159, 167, 178],
...,
[ 76, 62, 61],
[ 50, 39, 37],
[ 61, 51, 49]],
[[161, 171, 181],
[158, 168, 178],
[152, 162, 172],
...,
[ 75, 61, 60],
[ 43, 32, 30],
[ 55, 44, 42]]], dtype=uint8)
array([[[ 55, 56, 50],
[ 57, 58, 52],
[ 59, 60, 54],
...,
[137, 95, 35],
[ 91, 54, 27],
[ 62, 68, 56]],
[[ 56, 57, 51],
[ 57, 58, 52],
[ 59, 60, 54],
...,
[120, 80, 28],
[ 85, 53, 32],
[ 56, 63, 55]],
[[ 56, 57, 51],
[ 57, 58, 52],
[ 59, 60, 54],
...,
[ 91, 56, 14],
[ 83, 57, 44],
[ 47, 57, 56]],
...,
[[158, 166, 177],
[163, 171, 182],
[164, 172, 183],
...,
[ 74, 63, 61],
[ 53, 42, 40],
[ 60, 50, 48]],
[[160, 168, 179],
[162, 170, 181],
[159, 167, 178],
...,
[ 76, 62, 61],
[ 50, 39, 37],
[ 61, 51, 49]],
[[161, 171, 181],
[158, 168, 178],
[152, 162, 172],
...,
[ 75, 61, 60],
[ 43, 32, 30],
[ 55, 44, 42]]], dtype=uint8)
Now that you know how to load annotate and save images lets start manually annotating some images

# Load as many images as you like and start annotating objects in the image,
# Before annotating the images make a copy of the image so we keep the original without changes
# Use the format proposed in the example above
cv2.rectangle(clone,(100,200),(400,500), green, 3)
plt.figure( figsize = (20,15))
plt.imshow(clone)
<matplotlib.image.AxesImage at 0x16f2d638340>
# Display the images that you have annotated above, both versions, the original one and the annotated one
plt.figure( figsize = (20,15))
plt.imshow(rgb_img)
<matplotlib.image.AxesImage at 0x16f2c380250>
With all that you accomplished to do above you are now ready to do your first mini project!, you might need to learn some more new things to accomplish some of the harder levels below, it is very important to get used to the openCV docs
or any other.